// pages/music-player/index.js

import audioCTX from "../../store/audioctx"
import {audioStore} from '../../store/audioctx'
const playModule = {
  0:'order', // 列表循环
  1:'repeat', // 单曲循环
  2:'random' // 乱序
}
Page({

  data: {
    musicInfo: {},
    lyricList: [],
    currentLyricIndex: 0,
    currentTime: 0,
    totalTime: 0,

    scrollTop: 0,
    isDragging: false,
    
    current: 0,
    swiperHeight: 0,
    
    status: false, // 播放状态
    playModule: 'order', // 播放状态(中文)
    playModuleIndex: 0,

    isMoving: false,
    sliderValue: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const globalData = getApp().globalData
    const swiperHeight = globalData.screenHeight - globalData.statusBarHeight - 44
    this.setData({
      swiperHeight
    })
    audioStore.onStates(['musicInfo','lyricList','totalTime'], ({musicInfo,lyricList,totalTime}) => {
      if (musicInfo != undefined) {
        this.setData({
          musicInfo,
        })
      }
      if (lyricList != undefined) {
        this.setData({
          lyricList,
        })
      }
      if (totalTime != undefined) {
        this.setData({
          totalTime,
        })
      }
    })
    audioStore.onState('status', res => {
      this.setData({
        status: res,
      })
    })
    audioStore.onState('playModuleIndex', playModuleIndex => {
      this.setData({playModule: playModule[playModuleIndex], playModuleIndex})
    })
    audioStore.onStates(['currentTime', 'currentLyricIndex'], ({currentTime, currentLyricIndex}) => {
      if (currentTime) {
        if (!this.data.isMoving) {
          let sliderValue = currentTime / this.data.totalTime * 100
          this.setData({
            currentTime,
            sliderValue
          })
        }
      }
      if (currentLyricIndex) {
        this.setData({currentLyricIndex})
        let scrollTop = currentLyricIndex * 35
        if (!this.data.isDragging) {
          this.setData({
            scrollTop
          })
        }
      }

    })
  },
  changeMusicStatus() {
    audioStore.dispatch('changeAudioStatus', this.data.status)
  },
  changePlayModule() {
    audioStore.dispatch('changePlayModuleIndexAction', this.data.playModuleIndex)
  },
  changCurrentPlaySong(e) {
    audioStore.dispatch('changCurrentPlaySong', e.currentTarget.dataset.type)
  },
  sliderChange(e) {
    const sliderValue = e.detail.value
    const currentTime = this.data.totalTime * sliderValue / 100
    audioCTX.pause()
    audioCTX.seek(currentTime / 1000)
    console.log(sliderValue);
    // audioCTX.play()
    this.setData({
      currentTime,
      sliderValue,
      isMoving: false
    })
  },
  sliderChanging(e) {
    const sliderValue = e.detail.value
    const currentTime = this.data.totalTime * sliderValue / 100
    this.setData({
      isMoving: true,
      currentTime
    })
  },
  binddragstart() {
    this.setData({
      isDragging: true
    })
  },
  binddragend() {
    this.setData({
      isDragging: false
    })
  },
  currentSwiperItemChange(e) {
    this.setData({
      current: e.detail.current
    })
  },
  changeSwiperCurrent(e) {
    const {
      currentTarget: {
        dataset: {
          index
        }
      }
    } = e
    this.setData({
      current: index
    })
  }
})